//==============================================================================
//
//               Copyright (C) 2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2007-09-20 08:10:15 $ $Revision: 1.10 $
//
//==============================================================================

#include "ofc/config.h"

#include "ofc/DTextScreen.h"
#include "ofc/DScreenHandler.h"

#include "DInc.h"
#include "DTest.h"


#ifdef HAVE_DTEXTSCREEN

//
// Screen Handler class
// 
@interface DHandlerTest : Object <DScreenHandler>
{
@private
  int          _counter; // the check counter
  DTextScreen *_screen;  // the screen
}

- (DHandlerTest *) init :(DTextScreen *) screen;
- (BOOL) keyPress :(DKey *) key :(int) state;
- (BOOL) mouseMove :(unsigned) x :(unsigned) y;
- (BOOL) mouseButtonPress :(DKey *) button :(int) state;
- (BOOL) screenResize :(unsigned) maxX :(unsigned) maxY;
- (BOOL) screenFocus :(BOOL) focus;
- (BOOL) screenRedraw :(unsigned) minX :(unsigned) maxX :(unsigned) minY :(unsigned) maxY;
        
@end

@implementation DHandlerTest

  - (DHandlerTest *) init :(DTextScreen *) screen
{
  [super init];
  
  _counter = 0;
  _screen  = screen;
  
  return self;
}

- (BOOL) keyPress :(DKey *) key :(int) state
{
  DText *info = [key toText];

  [_screen startDrawing];
  [_screen cursor    :5 :1];
  [_screen writeText :[[info ljust :10] cstring]];
  [_screen stopDrawing];
  
  [info free];
  
  return ([key key] != 'q');
}

- (BOOL) mouseMove :(unsigned) x :(unsigned) y
{
  DText *info = [DText new];
  
  [info format :"x%d y%d", x, y];
  
  [_screen startDrawing];
  [_screen cursor :30 :1];
  [_screen writeText :[[info ljust :10] cstring]];
  [_screen stopDrawing];
  
  [info free];
  
  return YES;
}

- (BOOL) mouseButtonPress :(DKey *) button :(int) state
{
  DText *info = [button toText];
  
  [_screen startDrawing];
  [_screen cursor :47 :1];
  [_screen writeText :[[info ljust :10] cstring]];
  
  [info format :"%d", state];
  [_screen cursor :63 :1];
  [_screen writeText :[[info ljust :10] cstring]];
  [_screen stopDrawing];
  
  [info free];
  
  return YES;
}

- (BOOL) screenResize :(unsigned) maxX :(unsigned) maxY
{
  DText *info = [DText new];
  
  [_screen startDrawing];
  
  [info format :"Resize: %d - %d", maxX, maxY];
  
  [_screen writeText :5 :2 :[info cstring]];
  
  [_screen stopDrawing];
  
  [info free];

  return YES;
}

- (BOOL) screenFocus :(BOOL) focus
{
  [_screen startDrawing];
  [_screen cursor    :5 :1];
  if (focus)
    [_screen writeText :"focus!"];
  else
    [_screen writeText :"doei!"];
  [_screen stopDrawing];
        
  return YES;
}

- (BOOL) screenRedraw :(unsigned) minX :(unsigned) maxX :(unsigned) minY :(unsigned) maxY
{
  return YES;
}

@end

#endif  

void DTextScreen_test()
{
#ifdef HAVE_DTEXTSCREEN
  
  DTextScreen  *screen  = [DTextScreen new];
  DColor       *fgc     = [DColor new];
  DColor       *bgc     = [DColor new];
  DHandlerTest *handler = [DHandlerTest alloc];
  
  STARTTEST();

  [fgc textColor :DCLR_YELLOW];
  [bgc textColor :DCLR_BLUE];
  
  TEST([screen open :fgc :bgc]); // Running under msys (win32) will result in failure

  if ([screen isOpen])
  {
    [handler init :screen];

    TEST([screen startDrawing]);
    [screen cursor :1 :1];
    [screen writeText :"Key:"];
    [screen cursor :15 :1];
    [screen writeText :"Mouse location:"];
    [screen cursor :40 :1];
    [screen writeText :"button:"];
    [screen cursor :57 :1];
    [screen writeText :"state:"];
    [screen stopDrawing];
  
    [screen beep];

    TEST([screen startDrawing]);
  
    [screen clip :3 :3 :57 :[screen maxY]-3];
    [fgc textColor :DCLR_BLACK];
    [bgc textColor :DCLR_RED];
    TEST([screen color :fgc :bgc]);
    [screen clear];

    [screen clip :6 :6 :54 :[screen maxY]-6];
    [fgc textColor :DCLR_BLACK];
    [bgc textColor :DCLR_GREEN];
    TEST([screen color :fgc :bgc]);
    [screen clear];

    [screen clip :9 :9 :51 :[screen maxY]-9];
    [fgc textColor :DCLR_BLACK];
    [bgc textColor :DCLR_BLUE];
    TEST([screen color :fgc :bgc]);
    [screen clear];
  
    [screen stopDrawing];

  
    [screen cursor :10 :0];
    TEST([screen tell] == 10);
  
    [screen cursor :10 :5];
    TEST([screen tell] == (10 + 5 * [screen columns]));
  
    [screen skip :[screen columns] - 1];
    TEST([screen tell] == (9 + 6 * [screen columns]));
  
    TEST([screen cursorX] == 9);
    TEST([screen cursorY] == 6);
  
    [screen seek :(5 + 15 * [screen columns]) :0];
  
    TEST([screen cursorX] == 5);
    TEST([screen cursorY] == 15);
  
    [screen seek :(2 + 2 * [screen columns]) :1];
  
    TEST([screen cursorX] == 7);
    TEST([screen cursorY] == 17);
  
    [screen seek :0 :2];
  
    TEST([screen cursorX] == [screen maxX]);
    TEST([screen cursorY] == [screen maxY]);
  
    [screen startDrawing];
  
    [fgc textColor :DCLR_YELLOW];
    [bgc textColor :DCLR_BLUE];
  
    TEST([screen color :fgc :bgc]);
    
    [screen drawHLine :3 :2 :57];
    [screen drawVLine :[screen maxY] - 3];
  
    [screen drawLine :3 :3 :56 :[screen maxY] - 3];
    TEST([screen cursorX] == 56);
    TEST([screen cursorY] == [screen maxY] - 3);

    [screen drawHLine :3];
    [screen drawLine  :56 :3];
    [screen drawVLine :3 :[screen maxY] - 3 :2];
    
    [screen cursor :DDW_CURSOR_OFF];

    [screen blit :60 :4 :screen :1 :1 :10 :10];

    [screen writeText :2 :[screen maxY]-1 :"Press q to quit.."];
    
    [screen stopDrawing];

    [screen screenHandler :handler];
  
    [screen waitEvents];
    
    [screen cursor :1 :[screen maxY]-1];
    
    [screen cursor :DDW_CURSOR_ON];
  }
  
  [screen free];
  [fgc    free];
  [bgc    free];
  
  STOPTEST();
#endif
}

